﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GameLogic.Controls;
using GameLogic.Logic;

namespace Bounce.Phone
{
    public partial class BounceGame : UserControl, IGameFeatures
    {
        GameLoop gameLoop; // GameLoop behavior for collision and animation

        public event EventHandler<ScoresChangedEventArgs> ScoresChanged;
        public event EventHandler<LivesChangedEventArgs> LivesChanged;
        public event EventHandler<TimeChangedEventArgs> TimeChanged;
        public event EventHandler<GameOverEventArgs> GameOver;

        public BounceGame()
        {
            // Required to initialize variables
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(OnLoaded);
        }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            // Find the GameLoop
            GameLoopFinder glf = new GameLoopFinder(LayoutRoot);
            gameLoop = glf.GameLoop;
        }

        public void Start()
        {
            // Show GameReady screen
            VisualStateManager.GoToState(this, "GameReady", false);
        }

        public void Stop()
        {
            if (gameLoop != null)
            {
                gameLoop.ResetGameLoop();               
            }
                
        }

        public void Pause()
        {
            if (gameLoop != null)
            {
                gameLoop.PauseGameLoop();
            }
        }

        public void Resume()
        {
            if (gameLoop != null)
            {
                gameLoop.StartGameLoop();

                // Show GameRunning screen
                VisualStateManager.GoToState(this, "GameRunning", false);
            }
        }

        public void OnScoresChanged(int scores)
        {
            // Fire ChangeScoresEvent
            if (ScoresChanged != null)
            {
                ScoresChangedEventArgs args = new ScoresChangedEventArgs();
                args.Scores = scores;
                ScoresChanged(this, args);
            }
        }

        public void OnLivesChanged(int lives)
        {
            if (lives < 0)
            {
                this.Stop();

                // Show GameRunning screen
                VisualStateManager.GoToState(this, "LifeLost", false);
            }

            // Fire ChangeLivesEvent
            if (LivesChanged != null)
            {
                LivesChangedEventArgs args = new LivesChangedEventArgs();
                args.Lives = lives;
                LivesChanged(this, args);
            }
        }

        private void _startGameBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.Stop();
            this.Resume();            
        }

        private void _continueGameBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.Stop();
            this.Resume();
        }

        public bool IsTimeBased
        {
            get { return false; }
        }

        public bool IsLifeBased
        {
            get { return true; }
        }

        public string GameName
        {
            get { return "Bounce"; }
        }

        public string DeveloperName
        {
            get { return "Dennis Sasse"; }
        }

        public string CompanyName
        {
            get { return "Microsoft Deutschland GmbH"; }
        }

        public string Description
        {
            get { return String.Empty; }
        }

        public TimeSpan GameDuration
        {
            get { return new TimeSpan(); }
        }

        public Uri SettingsPage
        {
            get { return null; }
        }
    }
}